﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Microsoft.Live.ServerControls.VE;

namespace Microsoft.Live.ServerControls.Web
{
    public partial class DataTest : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShapeLayer mylayer = new ShapeLayer();
                mylayer.Description = "mylayer";
                Shape myshape = new Shape(ShapeType.Pushpin, new LatLongWithAltitude(40, -105));
                myshape.Title = "<a href=\"#\" onclick=\"alert('hello')\">click me</a>";
                //myshape.ID = "test";
                mylayer.AddShape(myshape);
                Map1.AddShapeLayer(mylayer);
            }
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(1);
            existinglayer.AddShape(new Shape(ShapeType.Pushpin, new LatLongWithAltitude(36, -115)));
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(1);
            existinglayer.DeleteAllShapes();
        }

        protected void ButtonHide_Click(object sender, EventArgs e)
        {
            ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(1);
            existinglayer.Hide();
        }

        protected void ButtonShow_Click(object sender, EventArgs e)
        {
            ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(1);
            existinglayer.Show();
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(1);
            if (existinglayer != null && existinglayer.GetShapeCount() > 0)
            {
                Shape existingshape = existinglayer.GetShapeByIndex(0);
                existingshape.Title = "modified: " + existingshape.ID;
            }
        }

        protected void ButtonNewShapeAdd_Click(object sender, EventArgs e)
        {
            ShapeLayer newlayer = new ShapeLayer();
            newlayer.Title = "newlayer";
            newlayer.AddShape(new Shape(ShapeType.Pushpin, new LatLongWithAltitude(45, -105)));
            Map1.AddShapeLayer(newlayer);
        }

        protected void ButtonNewShapeDelete_Click(object sender, EventArgs e)
        {
            for (int x=0;x<Map1.GetShapeLayerCount();x++)
            {
                ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(x);
                if (existinglayer.Title == "newlayer")
                {
                    //existinglayer.DeleteAllShapes();
                    Map1.DeleteShapeLayer(existinglayer.ID);
                }
            }
        }

        protected void ButtonNewShapeHide_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < Map1.GetShapeLayerCount(); x++)
            {
                ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(x);
                if (existinglayer.Title == "newlayer")
                {
                    existinglayer.Hide();
                }
            }
        }

        protected void ButtonNewShapeShow_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < Map1.GetShapeLayerCount(); x++)
            {
                ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(x);
                if (existinglayer.Title == "newlayer")
                {
                    existinglayer.Show();
                }
            }
        }

        protected void ButtonNewShapeModify_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < Map1.GetShapeLayerCount(); x++)
            {
                ShapeLayer existinglayer = Map1.GetShapeLayerByIndex(x);
                if (existinglayer.Title == "newlayer")
                {
                    if (existinglayer.GetShapeCount() > 0)
                    {
                        Shape existingshape = existinglayer.GetShapeByIndex(0);
                        LatLongWithAltitude newpos = new LatLongWithAltitude(existingshape.Points[0].Latitude + 1, existingshape.Points[0].Longitude);
                        List<LatLongWithAltitude> points = new List<LatLongWithAltitude>();
                        points.Add(newpos);
                        existingshape.Points = points;
                    }
                }
            }
        }

        protected void ButtonBaseShapeAdd_Click(object sender, EventArgs e)
        {
            Map1.AddShape(new Shape(ShapeType.Pushpin, new LatLongWithAltitude(45, -115)));
        }

        protected void ButtonBaseShapeDelete_Click(object sender, EventArgs e)
        {
            Map1.DeleteAllShapes();
        }

        protected void ButtonBaseShapeHide_Click(object sender, EventArgs e)
        {
            Map1.HideAllShapeLayers();
        }

        protected void ButtonBaseShapeShow_Click(object sender, EventArgs e)
        {
            Map1.ShowAllShapeLayers();
        }

        protected void ButtonBaseShapeModify_Click(object sender, EventArgs e)
        {
            Map1.ShowMessage("Impossible to iterate base shape layer");
        }

        protected void ButtonTileAdd_Click(object sender, EventArgs e)
        {
            LatLongRectangle bound = new LatLongRectangle();
            bound.TopLeftLatLong = new LatLongWithAltitude(49, -123);
            bound.BottomRightLatLong = new LatLongWithAltitude(47,-121);
            List<LatLongRectangle> bounds = new List<LatLongRectangle>();
            bounds.Add(bound);
            TileSourceSpecification tileSourceSpec = new TileSourceSpecification();
            tileSourceSpec.ID = "Demo";
            tileSourceSpec.TileSource = "http://dev.live.com/virtualearth/sdk/layers/lidar/%4.png";
            tileSourceSpec.NumServers = 1;            
            tileSourceSpec.Bounds = bounds;            
            tileSourceSpec.MinZoom = 10;            
            tileSourceSpec.MaxZoom = 18;            
            tileSourceSpec.Opacity = 0.8;            
            tileSourceSpec.ZIndex = 100;
            Map1.AddTileLayer(tileSourceSpec, true);
            //setmapview
            Map1.Center = new LatLongWithAltitude(48.03, -122.4);
            Map1.ZoomLevel = 12;

        }

        protected void ButtonTileDelete_Click(object sender, EventArgs e)
        {
            Map1.DeleteTileLayer("Demo");
        }

        protected void ButtonTileHide_Click(object sender, EventArgs e)
        {
            Map1.HideTileLayer("Demo");
        }

        protected void ButtonTileShow_Click(object sender, EventArgs e)
        {
            Map1.ShowTileLayer("Demo");
        }

        protected void ButtonTileModify_Click(object sender, EventArgs e)
        {
            TileSourceSpecification existinglayer =  Map1.GetTileLayerByID("Demo");
            existinglayer.Opacity = 0.2;
            existinglayer.MinZoom = 15;
        }

        protected void ButtonImportGeoRSS_Click(object sender, EventArgs e)
        {
            ShapeSourceSpecification spec = new ShapeSourceSpecification(DataType.GeoRSS, "http://localhost:49171/georsstest.xml", new ShapeLayer());
            Map1.ImportShapeLayerData(spec, "testfunction", true);
        }

        protected void ButtonImportKML_Click(object sender, EventArgs e)
        {
            ShapeSourceSpecification spec = new ShapeSourceSpecification(DataType.ImportXML, "http://dsc.discovery.com/utilities/googleearth/nationalparks/nationalparks.kml", new ShapeLayer());
            Map1.ImportShapeLayerData(spec, null, true);
        }

        protected void ButtonImportLivedotcom_Click(object sender, EventArgs e)
        {
            ShapeSourceSpecification spec = new ShapeSourceSpecification(DataType.VECollection, "54139577DC329980!625", new ShapeLayer());
            Map1.ImportShapeLayerData(spec, "notvalid", true);
        }
    }
}